package net.love5z.web.interceptor;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.love5z.web.common.utils.ServletUtils;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginCheckInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        HttpSession session = request.getSession(true);
        Boolean isLogin = (Boolean) session.getAttribute("SESSION_IS_LOGIN");

        String requestURL = request.getRequestURL().toString();

        if (isLogin == null || isLogin.booleanValue() == false) {
            response.sendRedirect(ServletUtils.getBaseUrl(request) + "/user/login.aspx"
                                  + "?redirect=" + URLEncoder.encode(requestURL, "UTF-8"));
            return false;
        }

        return true;

    }
}
